//
//  NotesViewController.m
//  School+
//
//  Created by Sean Sebastian on 5/20/16.
//  Copyright © 2016 Sean Sebastian. All rights reserved.
//

#import "NotesViewController.h"
#import "NoteCell.h"
#import "MemorandumModel.h"
#import "BQLModelTool.h"
#import "AddNote.h"
#import "EditNote.h"
#import "UITableView+EmptyData.h"

#define RGB(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
#define shitYellow RGB(253, 178, 50)

#define kScreenWidth ([UIScreen mainScreen].bounds.size.width)
#define kScreenHeight ([UIScreen mainScreen].bounds.size.height)

#define iphone6_ScreenWidth 375.0
#define iphone6_ScreenHeight 667.0

#define kWindowWidth ([UIScreen mainScreen].bounds.size.width)
#define kWindowHeight ([UIScreen mainScreen].bounds.size.height)

#define kWidthRatio kScreenWidth/iphone6_ScreenWidth
#define kHeightRatio kScreenHeight/iphone6_ScreenHeight




@interface NotesViewController () <UITableViewDelegate , UITableViewDataSource>

@property (nonatomic , weak) UITableView *MainTableView;
@property (nonatomic, strong) NSMutableArray *modalsArrM;
@property (nonatomic, strong) FMDatabase *fmdb;

@end

@implementation NotesViewController

- (NSMutableArray *)modalsArrM {
    if (!_modalsArrM) {
        _modalsArrM = [[NSMutableArray alloc] init];
    }
    return _modalsArrM;
}

- (void)viewWillAppear:(BOOL)animated
{
    [self initNoteData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"备忘录";
    UIBarButtonItem *addBtn=[[UIBarButtonItem alloc] initWithTitle:@"新建" style:UIBarButtonItemStylePlain target:self action:@selector(AddClick)]; // 新建按钮
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : shitYellow}]; // 标题颜色
    self.navigationController.navigationBar.tintColor = shitYellow; // 左右按钮颜色
    self.navigationItem.rightBarButtonItem=addBtn;
    
    [self setMainTableView];
}

- (void)backClick
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)setMainTableView
{
    UITableView *tb = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWindowWidth, kWindowHeight)];
    _MainTableView = tb;
    _MainTableView.dataSource = self;
    _MainTableView.delegate = self;
    [self.view addSubview:_MainTableView];
}

// 查询备忘录(初始化
- (void)initNoteData
{
    [self.modalsArrM removeAllObjects];
    NSArray *modals = [BQLModelTool queryData:nil];
    NSArray *tempArr = [[modals reverseObjectEnumerator] allObjects]; // 将数组倒序排列
    [self.modalsArrM addObjectsFromArray:tempArr];
    [_MainTableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    [tableView tableViewDisplayWitMsg:@"您还没有写备忘录" ifNecessaryForRowCount:self.modalsArrM.count];
    return self.modalsArrM.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identfiy = @"cellID";
    NoteCell *cell = [tableView dequeueReusableCellWithIdentifier:identfiy];
    if(!cell){
        cell = [[NoteCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identfiy];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell setNoteData:self.modalsArrM[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    EditNote *edit = [[EditNote alloc] init];
    edit.model = self.modalsArrM[indexPath.row];
    [self.navigationController pushViewController:edit animated:YES];
}

#pragma cell编辑模式--------删除备忘录
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete){ //删除模式
        [BQLModelTool deleteData:self.modalsArrM[indexPath.row]];
        //[MainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    [self initNoteData]; // 重新查询数据刷新表格
}


#pragma  自定义左滑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 25 * kHeightRatio + 34.5;
}

// 新建备忘录
- (void)AddClick
{
    AddNote *add = [[AddNote alloc] init];
    [self.navigationController pushViewController:add animated:YES];
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
